package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.InputType;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.StringUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxCompoundButton;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.data.App;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.present.ChangePswPresenter;
import health.lm.com.router.ARouterConstant;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;

/**
 * 修改密码
 */
@Route(path = ARouterConstant.ChangePswActivity)
public class ChangePswActivity extends XActivity<ChangePswPresenter> {

    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.et_old_psw)
    EditText etOldPsw;
    @BindView(R2.id.cb_oldpwd)
    CheckBox cbOldpwd;
    @BindView(R2.id.et_new_psw)
    EditText etNewPsw;
    @BindView(R2.id.cb_newpwd)
    CheckBox cbNewpwd;
    @BindView(R2.id.et_new_psw2)
    EditText etNewPsw2;
    @BindView(R2.id.cb_newpwd2)
    CheckBox cbNewpwd2;
    @BindView(R2.id.tv_change_psw)
    TextView tvChangePsw;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_psw;
    }

    @Override
    public ChangePswPresenter newP() {
        return new ChangePswPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {

        //titlebar 返回按钮
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });
        //验证输入内容
        Observable<CharSequence> oldObservale = RxTextView.textChanges(etOldPsw).debounce(500, TimeUnit.MILLISECONDS);
        Observable<CharSequence> pswObservale = RxTextView.textChanges(etNewPsw).debounce(500, TimeUnit.MILLISECONDS);
        Observable<CharSequence> pswObservale2 = RxTextView.textChanges(etNewPsw2).debounce(500, TimeUnit.MILLISECONDS);
        Observable.combineLatest(oldObservale,  pswObservale, pswObservale2, (old, psw, psw2) -> {
            boolean isOld = old.toString().length()>0;
            boolean isNew = false;
            if (!StringUtils.isEmpty(psw.toString()) && !StringUtils.isEmpty(psw2.toString())) {
                isNew = StringUtils.equals(psw.toString(), psw2.toString());
                if (!isNew) {
                    getvDelegate().toastShort("两次输入的密码不一致");
                }else {
                    if(psw.length()<6){
                        getvDelegate().toastShort("请输入大于6位数的密码");
                        isNew = false;
                    }
                }
            }
            return isOld  && isNew;
        }).compose(bindToLife())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aBoolean -> RxView.enabled(tvChangePsw).accept(aBoolean));

        //登录监听
        RxView.clicks(tvChangePsw)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o ->getP().changePsw(etOldPsw.getText().toString().trim(),etNewPsw.getText().toString().trim(),etNewPsw2.getText().toString().trim()));
        //输入旧密码显示明文
        RxCompoundButton.checkedChanges(cbOldpwd)
                .compose(bindToLife())
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        etOldPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    } else {
                        etOldPsw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                });
        //新密码显示明文
        RxCompoundButton.checkedChanges(cbNewpwd)
                .compose(bindToLife())
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        etNewPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    } else {
                        etNewPsw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                });
        //确认新密码显示明文
        RxCompoundButton.checkedChanges(cbNewpwd2)
                .compose(bindToLife())
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        etNewPsw2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    } else {
                        etNewPsw2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                });
    }

    public void changePswSuccess() {
        App.getModel().clear();
        gotoActivity(ARouterConstant.LoginActivity,true);
    }


}
